package com.eltsistemas.dao;

import com.eltsistema.hibernate.HibernateUtil;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;

import com.eltsistemas.modelo.ItemDoServico;

public class ItemDoServicoDao {
	
	private Session session;
	
	public ItemDoServicoDao()
	{
            session = new HibernateUtil().getSession();
            session.beginTransaction();
	}
	
	public void adiciona(ItemDoServico item)
	{
            session.save(item);
            session.getTransaction().commit();
            session.close();
	}
	
	public List<ItemDoServico> lista()
	{
            List<ItemDoServico> itemList = session.createCriteria(ItemDoServico.class).list();
            session.close();
            return itemList;
	}
	
	public ItemDoServico exibe(ItemDoServico item)
	{
            ItemDoServico encontradoItemDoServico = (ItemDoServico) session.load(ItemDoServico.class, item.getId());
            return encontradoItemDoServico;
	}
	
	public void altera(ItemDoServico item)
	{
            session.update(item);
            session.getTransaction().commit();
            session.close();
        }
	
	public void remover(ItemDoServico item)
	{
            session.delete(item);
            session.getTransaction().commit();
            session.close();
	}

	public ItemDoServico buscaCodigo(String nome)
        {
             System.out.println("T: "+nome);
            
            Query query = this.session.createQuery("from ItemDoServico where nome = :nome");
            query.setString("nome", nome);
            
            System.out.println("T");
				
            ItemDoServico encontradoItemDoServico = (ItemDoServico) query.uniqueResult();
            session.close();
            return encontradoItemDoServico;	
	}
}
